package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppJwtUtil;
import com.heima.media.dto.LoginDto;
import com.heima.media.entity.WmUser;
import com.heima.media.mapper.WmUserMapper;
import com.heima.media.service.IWmUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 自媒体用户信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-06-24
 */
@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements IWmUserService {

    @Override
    public ResponseResult<WmUser> saveWmUser(WmUser user) {
        if (user == null || user.getApUserId() == null || StringUtils.isEmpty(user.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 保存自媒体用户
        // 判断当前用户是否已经创建自媒体账户,根据ap_user_id判断
        // 根据ap_user_id查询
        LambdaQueryWrapper<WmUser> query = new LambdaQueryWrapper<>();
        query.eq(WmUser::getApUserId,user.getApUserId());
        WmUser one = this.getOne(query);
        if (one != null){
            return ResponseResult.okResult(one);
        }
        //没有就创建
        user.setCreatedTime(new Date());
        this.save(user);
        return ResponseResult.okResult(user);


    }

    @Override
    public ResponseResult login(LoginDto dto) {
        // 非空判断
        if (dto == null || StringUtils.isEmpty(dto.getName()) || StringUtils.isEmpty(dto.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 根据用户名查询用户
        LambdaQueryWrapper<WmUser> query = new LambdaQueryWrapper<>();
        query.eq(WmUser::getName,dto.getName());
        WmUser one = this.getOne(query);
        // 判断用户名是否存在
        if (one == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 获取表中的盐
        String salt = one.getSalt();
        // 用前端传来的密码与盐拼接,使用MD5得到密码
        String password = dto.getPassword() + salt;
        String md5password = DigestUtils.md5DigestAsHex(password.getBytes());
        // 比对表中密码
        if (!md5password.equals(one.getPassword())){
            // 不一致,提示密码不正确
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }

        // 一致, 使用JWT生成token
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId",one.getId());
        String token = AppJwtUtil.getToken(map);
        //返回前端定义好的格式
        Map<String,Object> userMap = new HashMap<>();
        userMap.put("token",token);
        // 隐藏用户的敏感信息
        one.setPassword(null);
        one.setSalt(null);
        one.setPhone(null);
        userMap.put("user",one);
        return ResponseResult.okResult(userMap);


    }
}
